<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" ref="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item prop="key">
        <el-input v-model="dataForm.key" placeholder="船员/船东名称/船舶名称" clearable></el-input>
      </el-form-item>
      <el-form-item prop="aboardPost">
        <el-select v-model="dataForm.aboardPost" placeholder="上船职务">
          <el-option
            v-for="item in optionsAboardPost"
            :key="item.id"
            :label="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="dispatchStatus">
        <el-select v-model="dataForm.dispatchStatus" placeholder="派遣状态">
          <el-option
            v-for="item in optionsDispatchStatus"
            :key="item.id"
            :label="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetForm('dataForm')">重置</el-button>
        <!-- <el-button v-if="isAuth('generator:dispatchinfo:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <!-- <el-button v-if="isAuth('generator:dispatchinfo:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column
        prop="dispatchId"
        header-align="center"
        align="center"
        label="派遣标识">
      </el-table-column>-->
      <el-table-column prop="dispatchCrew" header-align="center" align="center" label="派遣船员"></el-table-column>
      <el-table-column prop="shipownerName" header-align="center" align="center" label="船东名称"></el-table-column>
      <el-table-column prop="vesselName" header-align="center" align="center" label="船舶名称"></el-table-column>
      <el-table-column prop="aboardPost" header-align="center" align="center" label="上船职务"></el-table-column>
      <el-table-column prop="aboardTime" header-align="center" align="center" label="上船时间"></el-table-column>
      <el-table-column prop="ashoreSite" header-align="center" align="center" label="上船地点"></el-table-column>
      <el-table-column prop="estimateBoardTime" header-align="center" align="center" label="在船时间"></el-table-column>
      <el-table-column
        prop="estimateDisembarkationTime"
        header-align="center"
        align="center"
        label="预计下船时间"
      ></el-table-column>
      <el-table-column prop="endTime" header-align="center" align="center" label="合同有效期"></el-table-column>
      <el-table-column
        prop="dispatchStatus"
        header-align="center"
        align="center"
        label="派遣状态"
      ></el-table-column>
      <!-- <el-table-column
        prop="shipType"
        header-align="center"
        align="center"
        label="船舶类型">
      </el-table-column>
      <el-table-column
        prop="navigationArea"
        header-align="center"
        align="center"
        label="航区">
      </el-table-column>
      <el-table-column
        prop="deadweightTonnage"
        header-align="center"
        align="center"
        label="载重吨位">
      </el-table-column>-->
      <el-table-column prop="remarks" header-align="center" align="center" label="备注"></el-table-column>
      <!-- <el-table-column
        prop="salaryEquirement"
        header-align="center"
        align="center"
        label="薪资">
      </el-table-column>
      <el-table-column
        prop="currency"
        header-align="center"
        align="center"
        label="币种">
      </el-table-column>
      <el-table-column
        prop="contractId"
        header-align="center"
        align="center"
        label="合同">
      </el-table-column>-->
      <!-- <el-table-column
        prop="affiliatedCompanyId"
        header-align="center"
        align="center"
        label="所属公司">
      </el-table-column>-->
      <el-table-column prop="createUserId" header-align="center" align="center" label="派遣人"></el-table-column>
      <!-- <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>-->
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.dispatchId)"
            v-if="scope.row.dispatchStatus === '待上船' || scope.row.dispatchStatus === '已上船'"
          >修改</el-button>
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.dispatchId)"
            v-if="scope.row.dispatchStatus === '待上船'"
          >删除</el-button>
          <el-button
            v-if="scope.row.dispatchStatus === '已上船'"
            type="text"
            size="small"
            @click="disembarkdHandle(scope.row.dispatchId)"
          >下船</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <disembarkd v-if="visibleDisembarkd" ref="disembarkd" @refreshDataList="getDataList"></disembarkd>
  </div>
</template>

<script>
import AddOrUpdate from "./dispatchinfo-add-or-update";
import Disembarkd from "./dispatchinfo-disembarkd";
export default {
  data() {
    return {
      dataForm: {
        key: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      optionsAboardPost: [],
      optionsDispatchStatus: [],
      visibleDisembarkdbox: '',
      visibleDisembarkd: false
    };
  },
  components: {
    AddOrUpdate,
    Disembarkd
  },
  activated() {
    // this.getDataList();
    if (this.$route.query.dispatchid !== undefined) {
      this.getDataListHome();
    } else {
      this.getDataList();
    }
  },
  methods: {
    getDataListHome() {
      var _this = this
      this.$http({
        url: this.$http.adornUrl("/generator/dispatchinfo/linkDisembarkationExpected"),
        method: "get",
        params: this.$http.adornParams({
          dispatchId: this.$route.query.dispatchid
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
      });
    },
    // dispatchStatusFmt(row) {
    //   if (row.dispatchStatus === "已下船") {
    //     this.visibleAddOrUpdate = "none";
    //   } else {
    //     this.visibleAddOrUpdate = "";
    //   }

    //   if (row.dispatchStatus === "待上船") {
    //     this.visibleDelete = "";
    //   } else {
    //     this.visibleDelete = "none";
    //   }

    //   if (row.dispatchStatus === "已上船") {
    //     this.visibleDisembarkdbox = "";
    //   } else {
    //     this.visibleDisembarkdbox = "none";
    //   }

    //   return (row.dispatchStatus = row.dispatchStatus);
    // },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/generator/dispatchinfo/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          aboardPost: this.dataForm.aboardPost,
          dispatchStatus: this.dataForm.dispatchStatus
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });

      this.$http({
        url: this.$http.adornUrl(`/generator/datadictionary/info/${1}`),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.optionsAboardPost = data && data.code === 0 ? data.list : [];
      });

      this.$http({
        url: this.$http.adornUrl(`/generator/datadictionary/info/${20}`),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.optionsDispatchStatus = data && data.code === 0 ? data.list : [];
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.dispatchId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/generator/dispatchinfo/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 下船
    disembarkdHandle(id) {
      this.visibleDisembarkd = true;
      this.$nextTick(() => {
        this.$refs.disembarkd.init(id);
      });
    }
  }
};
</script>
